﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：UsersManage.cs
  //文件功能描述：会员管理
  //创建人：赵国伟
  //生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class UsersManage
    {
        private readonly DALUsers dalUsers = new DALUsers();

        #region "查询指定用户的用户名信息； by 赵国伟 2012-03-18"
        /// <summary>
        /// 查询指定用户的用户名信息；
        /// </summary>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public List<IDNameObj> QueryList(List<string> userIds)
        {
            return dalUsers.QueryList(userIds);
        }
        #endregion

        #region "查询指定的用户信息并存为Hash结构； by 赵国伟 2012-03-18"
        /// <summary>
        /// 查询指定的用户信息并存为Hash结构；
        /// </summary>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public Dictionary<int, string> QueryListToHash(List<string> userIds)
        {
            List<IDNameObj> listOfData = QueryList(userIds);
            Dictionary<int, string> dict = new Dictionary<int, string>();
            foreach (IDNameObj d in listOfData)
                dict.Add(d.ID, d.Name);
            return dict;
        }
        #endregion

        #region "查询一条会员信息； by 赵国伟 2012-03-18"
        /// <summary>
        /// 查询一条会员信息；
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Users GetUserInfo(int userId)
        {
            return dalUsers.GetModel(userId);
        }
        #endregion



        #region "多条件查询会员信息； by 赵国伟 2012-03-18"
        /// <summary>
        /// 多条件查询会员信息；
        /// </summary>
        /// <param name="qo"></param>
        /// <returns></returns>
        public List<Users> QueryListAsPage(QueryUsersAsPage qo)
        {
            string cond = "1=1 ";
            string orderby = "";
            if (!string.IsNullOrEmpty(qo.KeyWords))
                cond += " and UserName like '%" + qo.KeyWords + "%' ";
            if (!string.IsNullOrEmpty(qo.StartDate))
                cond += " and RegisterDate >='" + qo.StartDate + "' ";
            if (!string.IsNullOrEmpty(qo.EndDate))
                cond += " and RegisterDate <='" + qo.EndDate + " 23:59:59.999' ";

            if (!string.IsNullOrEmpty(qo.Status))
                cond += " and Status='" + qo.Status + "' ";

            orderby = GetOrderBy(qo.Sorts);
            return dalUsers.QueryListAsPage(cond, orderby, qo.CurPage, qo.PageSize, out qo.TotalCount);
        }

        /// <summary>
        /// 返回排序语句；
        /// </summary>
        /// <param name="sorts"></param>
        /// <returns></returns>
        private string GetOrderBy(int sorts)
        {
            string orderby = "RegisterDate desc,UserID desc";
            switch (sorts)
            {
                case 1:
                    orderby = "RegisterDate ,UserID desc";
                    break;
                case 2:
                    orderby = "Logins desc,UserID desc";
                    break;
                case 3:
                    orderby = "Logins ,UserID desc";
                    break;
            }
            return orderby;
        }

        #endregion

        #region "注册指定的会员账号； by 赵国伟 2012-04-13"
        /// <summary>
        /// 注册指定的会员账号；
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public ReturnValue Register(Users u)
        {
            ReturnValue ret = new ReturnValue(ResultType.Fail, "注册失败！");
            //判断名字是否重复；
            List<Users> o = dalUsers.QueryList(1, "UserName ='" + u.UserName + "' or Email='" + u.Email + "'", "");
            if (o != null)
            {
                if (o[0].UserName == u.UserName)
                {
                    ret.Message = "用户名已存在！";
                }
                else if (o[0].Email == u.Email)
                {
                    ret.Message = "注册邮箱地址重复！";
                }
                return ret;
            }
            u.Status = UserStatus.正用.ToString();
            if ((u.UserID= dalUsers.Add(u)) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "注册成功！";

                //直接设置登录信息；
                Login(u);
            }
            return ret;
        }
        #endregion

        #region "通过邮箱激活指定的账号 by 赵国伟 2012-04-13"
        /// <summary>
        /// 通过邮箱激活码激活指定的账号；
        /// </summary>
        /// <param name="email"></param>
        /// <param name="VerifyCode"></param>
        /// <returns></returns>
        public ReturnValue SetActive(string email, string VerifyCode)
        {
            return null;
        }
        #endregion


        #region "通过用户名查找指定的会员信息 by 赵国伟 2012-03-18"
        /// <summary>
        /// 通过用户名查找指定的会员信息；
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public Users GetUserInfoByName(string userName)
        {
            List<Users> listOfUsers = dalUsers.QueryList(1, "UserName='" + userName + "'", "");
            if (listOfUsers != null)
                return listOfUsers[0];
            else
                return null;
        }
        #endregion

        #region "设置登录信息 by 赵国伟 2012-03-18"
        /// <summary>
        /// 设置登录信息
        /// </summary>
        public void Login(Users u)
        {
            if (u == null) return;

            Utility.SetCookies(CConst.Cookie_User_ID, u.UserID.ToString(), "", 0);
            Utility.SetCookies(CConst.Cookie_User_Name, u.UserName, "", 0);
            u.Logins++;
            u.LastLoginDate = DateTime.Now;
            u.LastLoginIP = Utility.GetClientIPAddress();
            dalUsers.Update(u);
        }
        #endregion

        #region "设置登出信息；by 赵国伟 2012-03-18"
        /// <summary>
        /// 设置登出信息；by 赵国伟 2012-03-18
        /// </summary>
        public void Logout()
        {
            Utility.SetCookies(CConst.Cookie_User_ID, "", "", 0);
            Utility.SetCookies(CConst.Cookie_User_Name, "", "", 0);
            Utility.SetCookies("tmpUserPwd", "", "", 0);
        }
        #endregion

        #region "判断是否处于登录状态 by 赵国伟 2012-03-18"
        /// <summary>
        /// 判断是否处于登录状态
        /// </summary>
        /// <returns></returns>
        public bool IsLogin()
        {
            if (Utility.GetCookies(CConst.Cookie_User_ID) == "")
                return false;
            else
                return true;
        }
        #endregion

        #region "获取当前已登录用户的状态信息； by 赵国伟 2012-03-18"
        /// <summary>
        /// 获取当前已登录用户的状态信息；
        /// </summary>
        /// <returns></returns>
        public Users GetLoginStatus()
        {
            Users u = new Users();
            u.UserID = Utility.Cast2int(Utility.GetCookies(CConst.Cookie_User_ID));
            u.UserName = Utility.GetCookies(CConst.Cookie_User_Name);
            return u;
        }
        #endregion

        #region "更新用户信息； by 明明"
        /// <summary>
        /// 更新用户信息；
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public ReturnValue UpdateUsersInfo(Users u)
        {
            ReturnValue ret = new ReturnValue();
            var sql = dalUsers.Update(u);
            if (sql > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "更新成功";
            }
            else
            {
                ret.Result = ResultType.Fail;
                ret.Message = "更新失败";

            }
            return ret;
        }
        #endregion

        #region "查询当前用户的基本信息； by 赵国伟 2012-08-10"
        /// <summary>
        /// 查询当前用户的基本信息；
        /// </summary>
        /// <returns></returns>
        public UserBaseInfo GetUserBaseInfo()
        {
            UserBaseInfo u = new UserBaseInfo();
            u.UserID = Utility.Cast2int(Utility.GetCookies(CConst.Cookie_User_ID));
            u.UserName = Utility.GetCookies(CConst.Cookie_User_Name);

            ShoppingCartsManage bllCarts = new ShoppingCartsManage();
            bllCarts.QueryCartGoodsCounts(out u.ShoppingCartsCounts, out u.ShoppingCartsMoneys);
            return u;
        }
        #endregion


        #region "判断指定的账号密码是否可以登录； by 赵国伟 2012-08-17"
        /// <summary>
        /// 判断指定的账号密码是否可以登录；
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public ReturnValue IsCanLogin(string userName, string pwd)
        {
            return dalUsers.IsCanLogin(userName, pwd);
        }
        #endregion

        #region "判断指定用户的密码是否正确； by 赵国伟 2012-08-18"
        /// <summary>
        /// 判断指定用户的密码是否正确；
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public bool IsPassWordCorrect(int userId, string passWord)
        {
            return dalUsers.IsPassWordCorrect(userId,Utility.MD5(passWord));
        }
        #endregion

        #region "修改指定账号的密码 by 赵国伟 2012-08-18"
        /// <summary>
        /// 修改指定账号的密码
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="passWord">用户密码</param>
        /// <returns></returns>
        public ReturnValue UpdatePassWord(int userId, string passWord)
        {
            return dalUsers.UpdatePassWord(userId,Utility.MD5(passWord));
        }
        #endregion

    }

}
